(function() {
	var app = angular.module('ConfigManagementApp', [ 'ui.bootstrap' ]);
	app
			.controller(
					'warningContectsController',
					function($http, $scope, $log, $uibModal) {
						$scope.insertComponent = {};
						$scope.deleteComponent = {};
						var controller = this;
						controller.parameter = {};
						controller.queryParameter = "";
						this.warningDataList = [];
						$scope.controller.ipss = [];
						$scope.controller.ipssNew = [];
						$scope.controller.selectedIpByWeb = [];
						$scope.controller.selectedIpByDb = [];
						$scope.controller.selectedIpByDbOldEmp = [];
						$scope.controller.id_ipaaa = "";// 页面数据List的id
						$scope.zTreeObjNew;// 树状图子节点 数据
						// 树状图记忆数组
						$scope.memoryArray = {
							"hostId" : "",
							"arrayList" : new Array()
						};
						$scope.conf = [];
						this.queryCondition = {
							"ip" : "",
							"page" : 1,
							"size" : 5,
						};
						this.paginationInfo = {
							"maxSize" : 5,
							"totalPages" : 0,
							"totalElements" : 0
						};

						$scope.clickEvent = function() {
							controller.queryCondition.page = controller.queryCondition.forwordPage;
							loadData();
						}

						// 跳转页数，回车事件
						$scope.enterEvent = function(e) {
							var keycode = window.event ? e.keyCode : e.which;
							if (keycode == 13) {
								$scope.clickEvent();
							}
						}

						this.pageChanged = function() {
							loadData();
						}
						// 完成全选全部选事件
						$("#check_all").click(
								function() {
									$(".check_item").prop("checked",
											$(this).prop("checked"));
								});
						// 选中单个元素，如果单个元素选择满了，那么第一个主元素也被选中
						$(document)
								.on(
										"click",
										".check_item",
										function() {
											if ($(".check_item:checked").length == $(".check_item").length) {
												$("#check_all").prop("checked",
														true);
											}
											if ($(".check_item:checked").length == 0) {
												$("#check_all").prop("checked",
														false);
											}
										});

						// 搜索事件
						this.search = function() {
							loadData();
						}

						// 重置事件
						this.reset = function() {
							$scope.queryParameter = "";
						}
						// 保存配置
						this.saveConfig = function() {
							var basePath = $scope.getAppBasePath();
							var conditons = [];
							var ips = "";
							$
									.each(
											$(".check_item:checked"),
											function() {
												var contectsObj = new Object();
												contectsObj.id = $(this).parents("tr").find("#id").val();
												contectsObj.ip = $(this).parents("tr").find("#ip_"+ contectsObj.id).val();
												contectsObj.warncontact = $(this).parents("tr").find("#warncontact").val();
												contectsObj.warncontype = $(this).parents("tr").find("#warncontype_sel").val();
												contectsObj.warnphone = $(this).parents("tr").find("#warnphone").val();
												contectsObj.warnemail = $(this).parents("tr").find("#warnemail").val();
												contectsObj.dangercontact = $(this).parents("tr").find("#dangercontact").val();
												contectsObj.dangercontype = $(this).parents("tr").find("#dangercontype_sel").val();
												contectsObj.dangerphone = $(this).parents("tr").find("#dangerphone").val();
												contectsObj.dangeremail = $(this).parents("tr").find("#dangeremail").val();
												contectsObj.seriouscontact = $(this).parents("tr").find("#seriouscontact").val();
												contectsObj.seriouscontype = $(this).parents("tr").find("#seriouscontype_sel").val();
												contectsObj.seriousphone = $(this).parents("tr").find("#seriousphone").val();
												contectsObj.seriousemail = $(this).parents("tr").find("#seriousemail").val();
												conditons.push(contectsObj);
											})
							// 如果没有选中的话
							if (conditons == "") {
								alert("请勾选需要修改的用户");
								return;
							} else {
								if (confirm("确认修改配置吗?")) {
									// 保存后台调用
									$
											.ajax({
												type : "POST",
												url : basePath
														+ "/data/updateWarningContectsInfo",
												dataType : "json",
												contentType : "application/json",
												data : JSON
														.stringify(conditons),
												success : function(data) {
													alert("保存成功");
													loadData();
												}
											});

								}
							}
						}

						// 删除配置
						this.delConfig = function() {
							var basePath = $scope.getAppBasePath();
							var conditons = new Array();
							var ips = "";
							$.each($(".check_item:checked"), function() {
								var id = "";
								id = $(this).parents("tr").find("#id").val();

								conditons.push(id);
								ips += $(this).parents("tr").find("#ip_sel")
										.val()
										+ ",";
							})
							ips = ips.substring(0, ips.length - 1);
							// 如果没有选中的话
							if (conditons == "") {
								alert("请勾选需要删除的用户");
								return;
							} else {

								if (confirm("确认删除配置吗?")) {
									// 保存后台调用
									$
											.ajax({
												type : "POST",
												url : basePath
														+ "/data/deleteWarningContectsInfo",
												dataType : "json",
												contentType : "application/json",
												data : JSON
														.stringify(conditons),
												success : function(data) {
													alert("删除成功");
													loadData();
												}
											});

								}
							}
						}
						// 数据列表中IP值获取
						this.selectIP = function(id) {
							$scope.selectIpList = {};
							$scope.controller.id_ipaaa = id;
							var modalSelectIp = $uibModal
									.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										// animation :
										// $scope.animationsEnabled,// 打开时的动画开关
										templateUrl : 'js/controller/configuration.management/selectIP.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'selectIpListController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									// size : "lg",// 模态框的大小尺寸
									});
						}

						// 新增配置
						this.insertConfig = function() {
							$scope.insertComponent = {};
							var modalInstanceAdd = $uibModal
									.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										// animation :
										// $scope.animationsEnabled,// 打开时的动画开关
										templateUrl : 'js/controller/configuration.management/insertConfig.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'insertConfigController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									// size : "lg",// 模态框的大小尺寸
									});

							modalInstanceAdd.result.then(function(result) {
								loadData();
							});
						}

						// 程序初期化
						this.init = function() {
							loadData();
						}

						this.init();
						// 加载数据
						function loadData() {
							if ($(".check_item:checked").length == 0) {
								$("#check_all").prop("checked", false);
							}
							var basePath = $scope.getAppBasePath();
							// 参数获取
							controller.queryCondition.ip = $scope.queryParameter
									|| "";
							// 后台调用

							$
									.ajax({
										type : "POST",
										url : basePath
												+ "/data/getWarningContectsInfo",
										dataType : "json",
										contentType : "application/json",
										data : JSON
												.stringify(controller.queryCondition),
										success : function(data) {
//											console.log(data);
											var jsonResult = angular.fromJson(data);
//											console.log(jsonResult)
											controller.warningDataList = jsonResult.list;
											controller.paginationInfo.totalPages = jsonResult.pages;
											controller.paginationInfo.totalElements = jsonResult.total;
											// 手动刷新
											$scope.$apply();
										}
									});

						}

					})
			.controller(
					'insertConfigController',
					function($scope, $uibModalInstance, $http, $log) {
						var basePath = $scope.getAppBasePath();
						var controller = this;
						// 程序初期化
						this.init = function() {
							loadData();
						}

						this.init();
						// 加载数据
						function loadData() {
							$scope.selectedSite = {};
							$
									.ajax({
										type : "POST",
										url : basePath
												+ "/data/getIpsAndSystemName",
										dataType : "json",
										contentType : "application/json",
										// data :
										// JSON.stringify(controller.queryCondition),
										success : function(data) {
											var jsonResult = angular
													.fromJson(data);
											$scope.insertComponents = jsonResult;
											$scope.insertComponent.warncontype = "0";
											$scope.insertComponent.dangercontype = "0";
											$scope.insertComponent.seriouscontype = "0";
											// 手动刷新
											$scope.$apply();
										}
									});
						}

						$scope.save = function() {
							var basePath = $scope.getAppBasePath();
							// INSERT参数定义
							insertComponent = {
								"ip" : "",
								"warncontact" : "",
								"warncontype" : "",
								"warnphone" : "",
								"warnemail" : "",
								"dangercontact" : "",
								"dangercontype" : "",
								"dangerphone" : "",
								"dangeremail" : "",
								"seriouscontact" : "",
								"seriouscontype" : "",
								"seriousphone" : "",
								"seriousemail" : ""
							};
							// INSERT值获取
							insertComponent.ip = $scope.selectedSite;
							insertComponent.warncontact = $scope.$parent.insertComponent.warncontact;
							insertComponent.warncontype = $scope.$parent.insertComponent.warncontype;
							insertComponent.warnphone = $scope.$parent.insertComponent.warnphone;
							insertComponent.warnemail = $scope.$parent.insertComponent.warnemail;
							insertComponent.dangercontact = $scope.$parent.insertComponent.dangercontact;
							insertComponent.dangercontype = $scope.$parent.insertComponent.dangercontype;
							insertComponent.dangerphone = $scope.$parent.insertComponent.dangerphone;
							insertComponent.dangeremail = $scope.$parent.insertComponent.dangeremail;
							insertComponent.seriouscontact = $scope.$parent.insertComponent.seriouscontact;
							insertComponent.seriouscontype = $scope.$parent.insertComponent.seriouscontype;
							insertComponent.seriousphone = $scope.$parent.insertComponent.seriousphone;
							insertComponent.seriousemail = $scope.$parent.insertComponent.seriousemail;
							if ($scope.$parent.insertComponent.warncontact == null
									&& $scope.$parent.insertComponent.dangercontact == null
									&& $scope.$parent.insertComponent.seriouscontact == null) {
								alert("请填写联系信息");
								return;
							}
							// 后台调用
							$.ajax({
								type : "POST",
								url : basePath
										+ "/data/insertWarningContectsInfo",
								dataType : "json",
								contentType : "application/json",
								data : JSON.stringify(insertComponent),
								success : function(data) {
									$uibModalInstance.close();
									alert("插入成功");
								}
							});

						};
						$scope.cancel = function() {
							// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
							$uibModalInstance.dismiss('cancel');
						};
					})
			.controller(
					'selectIpListController',
					function($scope, $uibModalInstance, $http, $log) {
						var basePath = $scope.getAppBasePath();
						var controller = this;
						controller.ipss = [];
						$scope.controller.selectedIpByDbOld = [];
						// 程序初期化
						this.init = function() {
							getTreeData();
						}

						this.init();

						// 获取树状图数据
						function getTreeData() {
							var basePath = $scope.getAppBasePath();
							var address = "";
							$
									.ajax({
										type : "POST",
										url : basePath + "/data/getIpTreeData",
										dataType : "json",
										contentType : "application/json",
										success : function(data) {
											var resultArrary = JSON.parse(data);

											var setting = {
												// 异步加载POST方法
												async : {
													enable : true,
													contentType : "application/json",
													dataType : "text",
													type : "post",
													url : basePath
															+ "/data/asyncGetIpTreeData",
													autoParam : [ "commentId" ],
													otherParam : [
															"hostId",
															$scope.controller.id_ipaaa ],
													dataFilter : ajaxDataFilter
												},
												// checkBox 效果
												check : {
													enable : true,
													chkStyle : "checkbox"
												},
												callback : {
													onAsyncSuccess : zTreeOnAsyncSuccess,
													onClick : zTreeOnClick,
													onExpand : zTreeOnExpand,// 点击节点前+号加载
													onCheck : zTreeOnCheck// 点击chckBox框加载
												}
											};

											function zTreeOnAsyncSuccess() {
											}
											// checked box 选中 取消时候，ip值的状态设置
											function zTreeOnCheck(event,
													treeId, treeNode) {
											}
											function zTreeBeforeAsync(treeId,
													treeNode) {
												return (treeNode.isParent == true);
											}
											;
											// 返回数据转成json
											function ajaxDataFilter(treeId,
													parentNode, responseData) {
												responseData = JSON
														.parse(responseData);
												// 获取已经被配置的Ip，并且设置checked为true
												for (var i = 0; i < responseData.length; i++) {
													if (responseData[i].checked == "true") {
														if (responseData[i].hostId == $scope.controller.id_ipaaa) {
															$scope.controller.selectedIpByDbOldEmp
																	.push(responseData[i]);
														}
													}
												}
												// 已经配置完成的IP集合。用于新配置的Ip过滤
												for (var n = 0; n < responseData.length; n++) {
													if (responseData[n].hostId == $scope.controller.id_ipaaa) {
														$scope.controller.selectedIpByDbOld
																.push(responseData[n].name);
													}
												}
												var checkedValue = [];
												var oldcheckedValue = $scope.controller.selectedIpByWeb;
												// 获取本次点击树上一次勾选的数据
												for (v = 0; v < oldcheckedValue.length; v++) {
													if (oldcheckedValue[v].hostId == $scope.controller.id_ipaaa) {
														checkedValue = oldcheckedValue[v].arrayList;
													}
												}

												// 上一次被勾选的数据再次被勾选中
												if (checkedValue != null
														&& checkedValue.length > 0) {
													for (var z = 0; z < responseData.length; z++) {
														for (var l = 0; l < checkedValue.length; l++) {
															if (checkedValue[l] == responseData[z].name) {
																responseData[z].checked = "true";
															} else {
															}
														}
													}

													// 已经配置入库 的IP地址，画面取消操作
													for (var h = 0; h < responseData.length; h++) {
														if ($.inArray(responseData[h].name,checkedValue) == -1) {
															responseData[h].checked = "flase";
														}
													}
												}
												return responseData;
											}
											;
											var zTreeObj = $.fn.zTree.init(
													$("#selectIP"), setting,
													resultArrary);
											$scope.zTreeObjNew = zTreeObj;
											// $("#search-bt").click(filter);
											function zTreeOnExpand(event,
													treeId, treeNode) {
												var nodes = zTreeObj
														.getSelectedNodes();
												if (nodes.length > 0
														&& treeNode.level != 0
														&& treeNode.level != 1) {
													zTreeObj
															.reAsyncChildNodes(
																	nodes[0],
																	"refresh");
												}
											}
											function zTreeOnClick(event,
													treeId, treeNode) {
											}

											// 过滤ztree显示数据
											var hiddenNodes;
											function filter() {
												// 显示上次搜索后背隐藏的结点
												zTreeObj.showNodes(hiddenNodes);
												var _keywords = $("#keyword")
														.val();
												if (_keywords == null
														|| _keywords == "") {
													zTreeObj
															.showNodes(hiddenNodes);
												} else {
													var nodes = zTreeObj
															.getNodesByParamFuzzy(
																	"name",
																	_keywords,
																	null); // 根据节点数据的属性搜索，获取条件模糊匹配的节点数据
																			// JSON
																			// 对象集合,(keywords)模糊匹配只能针对
																			// String
																			// 类型的数据
													if (nodes.length > 0) {
														zTreeObj.selectNode(nodes[0]); // 会让节点自动滚到到可视区域内
													}

													// 查找不符合条件的叶子节点
													function filterFunc(node) {
														var _keywords = $(
																"#keyword")
																.val();
														if (node.isParent
																|| node.name
																		.indexOf(_keywords) != -1) {
															return false;
														} else {
															return true;
														}
													}
													;

													// 获取不符合条件的叶子结点
													hiddenNodes = zTreeObj
															.getNodesByFilter(filterFunc);

													// 隐藏不符合条件的叶子结点
													zTreeObj
															.hideNodes(hiddenNodes);
												}
											}
											;
										}
									});
						}
						$scope.save = function() {
							document.getElementById('ip_'
									+ $scope.controller.id_ipaaa).value = "";
							// 获取取消的节点
							$scope.controller.selectedIpByDbOld;
							$scope.zTreeObjNew;
							// 获取选中的IP
							$scope.controller.ipssNew = [];
							var selectedIp = $scope.zTreeObjNew
									.getCheckedNodes(true);
							$scope.memoryArray = {
								"hostId" : "",
								"arrayList" : new Array()
							};
							for (var i = 0; i < selectedIp.length; i++) {
								if (selectedIp[i].chidren == undefined
										&& selectedIp[i].isParent == false) {
									// $scope.controller.ipss.push(selectedIp[i].name);
									$scope.controller.ipssNew
											.push(selectedIp[i].name);
									$scope.memoryArray.arrayList
											.push(selectedIp[i].name);
								}
							}
							for (var y = 0; y < $scope.controller.selectedIpByDbOld.length; y++) {
								if ($.inArray(
										$scope.controller.selectedIpByDbOld[y],
										$scope.controller.ipssNew) != -1) {
									removeByValue(
											$scope.controller.ipssNew,
											$scope.controller.selectedIpByDbOld[y]);
								} else {
									$scope.controller.ipssNew
											.push($scope.controller.selectedIpByDbOld[y]);
								}
							}
							//为画面设置影藏值
							document.getElementById('ip_'
									+ $scope.controller.id_ipaaa).value = $scope.controller.ipssNew;
							$scope.memoryArray.hostId = $scope.controller.id_ipaaa
							// 保持每一个hostId数据是唯一的最新的。
							if ($scope.controller.selectedIpByWeb != null
									&& $scope.controller.selectedIpByWeb.lentgh > 0) {
								for (var e = 0; e < $scope.controller.selectedIpByWeb.length; e++) {
									if ($scope.controller.selectedIpByWeb[e].hostId == $scope.memoryArray.hostId) {
										$scope.controller.selectedIpByWeb
												.split(e);
									}
									$scope.controller.selectedIpByWeb
											.push($scope.memoryArray);
								}
							} else {
								$scope.controller.selectedIpByWeb
										.push($scope.memoryArray);// 获取上次已经勾选但是还没有入库的数据
							}

							// 手动刷新
							var basePath = $scope.getAppBasePath();
							// INSERT参数定义
							selectComponent = {};
							$uibModalInstance.dismiss('cancel');
						};
						$scope.cancel = function() {
							// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
							$uibModalInstance.dismiss('cancel');
						};

						// 删除元素
						function removeByValue(arr, val) {
							for (var i = 0; i < arr.length; i++) {
								if (arr[i] == val) {
									arr.splice(i, 1);
									break;
								}
							}
						}
					})
})();
